package com.anjbo.controller.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.anjbo.common.RespDataObject;
import com.anjbo.common.RespStatus;
import com.anjbo.common.RespStatusEnum;
import com.anjbo.service.order.OrderThirdService;
import com.anjbo.utils.ConfigUtil;
import com.anjbo.utils.HttpUtil;
import com.anjbo.utils.JsonUtil;
import com.anjbo.utils.MD5Utils;


@Controller
@RequestMapping("/app/order/orderThird")
public class OrderThirdController {
	@Resource
	private OrderThirdService orderThirdService;
	
	/**
	 * 第三方表新增数据接口
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping("/insertData")
	@ResponseBody
	public RespStatus add(HttpServletRequest request, @RequestBody Map<String, Object> param){
		RespStatus rs = new RespStatus();
		try {
			int rv = orderThirdService.add(param);
			if (rv < 0) {
				rs.setCode(RespStatusEnum.FAIL.getCode());
				rs.setMsg(RespStatusEnum.FAIL.getMsg());
				return rs;
			}
		} catch (Exception e) {
			e.printStackTrace();
			rs.setCode(RespStatusEnum.FAIL.getCode());
			rs.setMsg(RespStatusEnum.FAIL.getMsg());
			return rs;
		}
		rs.setCode(RespStatusEnum.SUCCESS.getCode());
		rs.setMsg(RespStatusEnum.SUCCESS.getMsg());
		return rs;
	}
	/**
	 * 已累计受理贷款（单）：凡是已提交给经理审批的单， 即算1单
	 * 已累计成功放款（亿）：节点为“已放款”的订单，取订单的【放款信息】中的‘放款金额’这个数据，系统所有订单的这个数据的总和。
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectLendingTotalAndAuditCount")
	public RespDataObject<Map<String,Object>> selectSuccessLendingTotal(HttpServletRequest request){
		RespDataObject<Map<String,Object>> result = new RespDataObject<Map<String,Object>>();
		result.setCode(RespStatusEnum.FAIL.getCode());
		result.setMsg(RespStatusEnum.FAIL.getMsg());
		try{
			String url = ConfigUtil.getStringValue("ANJBO_FC")+"/fc/order/ransomfloor/v/selectLendingTotalAndAuditCount";
			String date = new SimpleDateFormat("yyyymmdd").format(new Date());
			String key = MD5Utils.MD5Encode("fc.anjbo.com"+date);
			Map<String,String> params = new HashMap<String,String>();
			params.put("key",key);
			String statusString = HttpUtil.post(url, params);
			result = (RespDataObject<Map<String,Object>>) JsonUtil.parseJsonToObj(statusString,RespDataObject.class);
			Map<String,Object> map = result.getData();
			if(MapUtils.isEmpty(map))
				return result;
			
			Integer auditCount = MapUtils.getInteger(map, "auditCount",0);
			Double lendingTotal = MapUtils.getDouble(map, "lendingTotal",0d);
			auditCount += ConfigUtil.getIntegerValue("AUDITCOUNT");
			lendingTotal += ConfigUtil.getDoubleValue("LENDINGTOTAL");
			map.put("auditCount", auditCount+"");
			map.put("lendingTotal", lendingTotal+"");
			result.setData(map);
		} catch (Exception e){
			e.printStackTrace();
		}
		return result;
	}
}
